import { message } from 'antd';
import React, { useEffect } from 'react';
// import {history} from '@umijs/max';

const OAuthCallback: React.FC = () => {
  const redirectUri = `${window.location.origin}/`;

  useEffect(() => {
    // 获取响应头中的 token 信息
    const getTokenFromHeader = async () => {
      try {
        // const response = await fetch(window.location.href, {
        //   method: 'GET',
        //   credentials: 'include',
        // });
        // console.log(response.statusText)
        // console.log(response.url)
        // console.log(response.headers)
        // const accessToken = response.headers.get('Authorization')?.replace('Bearer ', '');
        // const refreshToken = response.headers.get('Refresh-Token');

        const urlParams = new URLSearchParams(window.location.search);
        const accessToken = urlParams.get('accessToken');
        const refreshToken = urlParams.get('refreshToken');

        if (accessToken) {
          localStorage.setItem('accessToken', accessToken);
          if (refreshToken) {
            localStorage.setItem('refreshToken', refreshToken);
          }
          message.success('Login successful!');
          // history.push("/");
          window.location.href = redirectUri;
        } else {
          message.error('Login failed. No token found.');
        }
      } catch (error) {
        message.error(`Login failed. ${error}`);
      }
    };

    getTokenFromHeader().then(() => {
      // 可以在这里添加一些后续处理逻辑，如果需要的话
    });
  }, []);

  return <div>Redirecting...</div>;
};

export default OAuthCallback;
